package com.seeyon.ctp.rest.resources;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import jersey.repackaged.com.google.common.collect.Maps;


import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.apps.kk.manager.restImpl.MeasuringEquipmentImpl;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.filemanager.dao.AttachmentDAO;
import com.seeyon.ctp.common.filemanager.manager.FileManager;
import com.seeyon.ctp.common.po.filemanager.Attachment;
import com.seeyon.ctp.common.po.filemanager.V3XFile;
import com.seeyon.ctp.services.FileUploadExporter;
import com.seeyon.ctp.services.ServiceException;
import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.kk.modules.ibatis.IbatisQueryDao;
import com.seeyon.kk.utils.CoreUtils;
import com.seeyon.kk.utils.ValueUtils;

/**
 * 开放给会议系统（天目神盾）使用
 * @author seeyon
 *
 */
@Path("/shkk/meeting")
public class MeetingFileUrlAccept extends BaseResource {
	private static final Log log = LogFactory.getLog(MeetingFileUrlAccept.class);	
	/**
	 * 
	 * @param params  会议结束后返回的获取会议资料的信息
	 * @return
	 */
	@POST
	@Path("/FileUrl")
	@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_HTML })
	@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_HTML })
	public void getFileByTmsdUrl(JSONObject params) {
		log.info("==========SHKK========会议系统（天目神盾）远程调用接口-->《/shkk/meeting/FileUrl》，CALL_TIME:"+DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		if(params!=null){
			  //获取到的新文件上传至会议附件栏
			  
			  //
			  String savePath="";
			  String loginNamef="";
			  String summaryId="1234";
			  String formmainId="";
			  Map<String,Object> swmap=Maps.newHashMap();
			  FileManager file=null;
			  AttachmentDAO attdao=null;
			  IbatisQueryDao dao=null;
			  //上传附件
		      FileUploadExporter fileUpload = new FileUploadExporter();
		      String fileid = null;
				try {
					fileid = fileUpload.processUpload(loginNamef, new String[] {savePath+swmap.get("doc").toString()});
				} catch (ServiceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			      V3XFile vfile = null;
				try {
					vfile = file.getV3XFile(Long.valueOf(fileid));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		      Long attId = UUIDLong.longUUID();
		      Attachment att = new Attachment(vfile);
		      att.setReference(Long.valueOf(summaryId));
		      att.setCategory(2);
		      att.setSubReference(Long.valueOf(attId));
		      att.setIdIfNew(); 
		      attdao.save(att);
		      Map<String, Object> filters = new HashMap<String, Object>();     
		      filters.put("attId", attId);
		      filters.put("fieldId", formmainId);         
		      dao.getSqlMapClientTemplate().update("shkkUpdateFormmainAtt.update", filters);
			
			
			
		}
		Map<String, Object> rsMap = new HashMap<String, Object>();
		// 调用系统协同 消息接口返回信息 告诉相关用户是否更新附件成功i
		
		
		
		
		
	}
}
